home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group98c.txt
/
000127_icon-group-sender _Thu Dec 10 17:13:11 1998.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
2KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id RAA07960
for icon-group-addresses; Thu, 10 Dec 1998 17:13:00 -0700 (MST)
Message-Id: <199812110013.RAA07960@baskerville.CS.Arizona.EDU>
Date: Thu, 10 Dec 1998 10:44:01 -0700
From: Gregg Townsend <gmt>
To: evans.nospam@gte.net, icon-group
Subject: Re: Formatting Reals
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
I use the code below for formatting real numbers into fixed-width
fields. It's not as general as C's printf, and it's interpreted,
but it works.
(And to answer another question: real numbers are implemented by
C's "double" datatype.)
---------------------------------------------------------------------------
Gregg Townsend Gould-Simpson Building gmt@cs.arizona.edu
Staff Scientist 1040 E. 4th St. 32 13 45N 110 57 16W
Dept. of Computer Science PO Box 210077 tel: +1 520 621 4325
The University of Arizona Tucson, AZ 85721-0077 fax: +1 520 621 4246
==============================================================================
procedure main() #: sample driver
every write(frn(&pi, 20, 0 to 16))
end
# frn(r, w, d) format real number r into a string with d digits
# after the decimal point; a result narrower than w
# characters is padded on the left with spaces.
# Fixed format is always used; there is no exponential
# notation.
#
# Defaults: w=0, d=0
$define MAXDECIMALS 25
procedure frn(r, w, d) #: format real number
local s
static mlist
initial every put(mlist := list(), 10.0 ^ (0 to MAXDECIMALS))
r := real(r) | runerr(102, r)
(/d := 0) | (d >:= MAXDECIMALS)
if r >= 0.0 then {
s := string(integer(r * mlist[d + 1] + 0.5))
s := right(s, *s < d + 1, "0")
}
else {
s := string(integer(-r * mlist[d + 1] + 0.5))
s := right(s, *s < d + 1, "0")
s := "-" || s
}
s := right(s, *s < (\w - 1))
return s ? (tab(-d) || "." || tab(0))
end